﻿using System.Collections.Generic;

namespace FrameOperateSync
{
    class RenderLogicManager
    {
        private Queue<FrameOperateData> frameOperateQueue = new Queue<FrameOperateData>();
        private LinkedList<FrameOperateData> frameRenderNodeList = new LinkedList<FrameOperateData>();
        private LinkedListNode<FrameOperateData> currentRenderNode = null;
        private int currentKeyFrameIndex = -1;

        public void RecvRenderData(FrameOperateData operateData)
        {
            lock (frameRenderNodeList)
            {
                frameRenderNodeList.AddLast(operateData);

                if (currentKeyFrameIndex == -1)
                {
                    currentRenderNode = frameRenderNodeList.First;
                }
                currentKeyFrameIndex = operateData.KeyFrameIndex;
            }
        }

        public void Reset()
        {
            frameRenderNodeList.Clear();
            currentRenderNode = null;
        }

        public void OnFrameRender(int keyFrameIndex, int frameIndex)
        {
            lock (frameRenderNodeList)
            {
                while (currentRenderNode != null && currentRenderNode.Value.KeyFrameIndex < currentKeyFrameIndex)
                {
                    Loger.Log($"nFrameRender:frameIndex = {frameIndex},curFrameIndex = {currentRenderNode.Value.FrameIndex},code = {currentRenderNode.Value.LogicCode}");

                    //RenderData.
                    RenderData.RecvRenderData(currentRenderNode.Value);
                    currentRenderNode = currentRenderNode.Next;
                }
            }
        }
    }
}
